package com.mall.commons.common;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Data
public class CommonResult<T> implements Serializable {
    /**
     * 编码：1成功，0和其它数字为失败
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String message;
    /**
     * 数据
     */
    private T data;
    /**
     * 动态数据
     */
    private Map map = new HashMap();
    /**
     * 成功，返回两个参数
     *
     * @Date: 2023-02-09 14:37
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param object
     * @return com.itheima.youdianda.common.R<T>
     */
    public static <T> CommonResult<T> success(T object) {
        CommonResult<T> r = new CommonResult<T>();
        r.data = object;
        r.code = 200;
        return r;
    }

    /**
     * 成功，返回三个参数
     *
     * @Date: 2023-02-11 21:30
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param object
     * @param msg
     * @return com.itheima.youdianda.common.R<T>
     */
    public static <T> CommonResult<T> success(T object, String msg) {
        CommonResult<T> r = new CommonResult<T>();
        r.data = object;
        r.code = 200;
        r.message = msg;
        return r;
    }
    /**
     * 失败
     *
     * @Date: 2023-02-09 14:38
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param msg
     * @return com.itheima.youdianda.common.R<T>
     */
    public static <T> CommonResult<T> error(String msg) {
        CommonResult r = new CommonResult();
        r.message = msg;
        r.code = 202;
        return r;
    }
    /**
     * 添加
     *
     * @Date: 2023-02-09 14:38
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param key
     * @param value
     * @return com.itheima.youdianda.common.R<T>
     */
    public CommonResult<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
